/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.contract.entity.RippleContractFundingRateHistory;
import com.pig4cloud.pigx.contract.service.RippleContractFundingRateHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 资金费率历史
 *
 * @author pigx code generator
 * @date 2022-09-11 13:55:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rippleContractFundingRateHistory")
@Api(value = "rippleContractFundingRateHistory", tags = "资金费率历史管理")
public class RippleContractFundingRateHistoryController {

    private final RippleContractFundingRateHistoryService rippleContractFundingRateHistoryService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param history 资金费率历史
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getRippleContractFundingRateHistoryPage(Page<RippleContractFundingRateHistory> page, RippleContractFundingRateHistory history) {
        QueryWrapper<RippleContractFundingRateHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("symbol_id", history.getSymbolId());
        wrapper.orderBy(true, false, "date_time");
        Page<RippleContractFundingRateHistory> page1 = rippleContractFundingRateHistoryService.page(page, wrapper);
        page1.getRecords().forEach(e -> {
            e.setFundingRate(e.getFundingRate()
                    .multiply(BigDecimal.TEN
                            .multiply(BigDecimal.TEN))
                    .setScale(2, BigDecimal.ROUND_HALF_UP));
        });
        return R.ok(page1);
    }


    /**
     * 通过id查询资金费率历史
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('contract_ripplecontractfundingratehistory_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(rippleContractFundingRateHistoryService.getById(id));
    }

    /**
     * 新增资金费率历史
     *
     * @param rippleContractFundingRateHistory 资金费率历史
     * @return R
     */
    @ApiOperation(value = "新增资金费率历史", notes = "新增资金费率历史")
    @SysLog("新增资金费率历史")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('contract_ripplecontractfundingratehistory_add')")
    public R save(@RequestBody RippleContractFundingRateHistory rippleContractFundingRateHistory) {
        return R.ok(rippleContractFundingRateHistoryService.save(rippleContractFundingRateHistory));
    }

    /**
     * 修改资金费率历史
     *
     * @param rippleContractFundingRateHistory 资金费率历史
     * @return R
     */
    @ApiOperation(value = "修改资金费率历史", notes = "修改资金费率历史")
    @SysLog("修改资金费率历史")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('contract_ripplecontractfundingratehistory_edit')")
    public R updateById(@RequestBody RippleContractFundingRateHistory rippleContractFundingRateHistory) {
        return R.ok(rippleContractFundingRateHistoryService.updateById(rippleContractFundingRateHistory));
    }

    /**
     * 通过id删除资金费率历史
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除资金费率历史", notes = "通过id删除资金费率历史")
    @SysLog("通过id删除资金费率历史")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('contract_ripplecontractfundingratehistory_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(rippleContractFundingRateHistoryService.removeById(id));
    }


    /**
     * 导出excel 表格
     *
     * @param rippleContractFundingRateHistory 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('contract_ripplecontractfundingratehistory_export')")
    public List<RippleContractFundingRateHistory> export(RippleContractFundingRateHistory rippleContractFundingRateHistory) {
        return rippleContractFundingRateHistoryService.list(Wrappers.query(rippleContractFundingRateHistory));
    }
}
